using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace One_Crappy_RPG
{
    public class Hero : Creature
    {
        public int NextLevel = 1000;
        const int MaxLevel = 99;
        public static int StepCounter = 0;
        public bool IsMoving = false;
        public bool InBattle;
        public Vector2 MoveTarget = Vector2.Zero;
        Vector2 Motion;
        Vector2 MoveSpeed = new Vector2(2.5f, 2.5f);
        public Vector2 MapPosition;
        public TileMap CurrentMap;

        public Hero()
        {
            MyTurn = false;
            Name = "Player";

            Stats.Strength = 10;
            Stats.CurrentHealth = 30;
            Stats.MaxHealth = 30;
            Stats.CurrentMP = 20;
            Stats.MaxMP = 20;
            Stats.Defense = 5;
            Stats.Speed = 10;
            Stats.Agility = 10;
            Stats.Intelligence = 10;
            Stats.MagicDefense = 10;

            SpellBook.Add("Minor Heal", new Heal1("Minor Heal", 5, this));
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            ClampToMap();

                //Not at our destination cell yet
                if (MapPosition != MoveTarget)
                {
                    IsMoving = true;

                    Motion = MoveTarget - MapPosition;
                    Motion.Normalize();

                    MapPosition += Motion * MoveSpeed;
                }
                else if (IsMoving) //We must be there
                {
                    IsMoving = false;
                    Hero.StepCounter++;
                }
        }

        public void ClampToMap()
        {
            Point destinationCell;

            MoveTarget.X = MathHelper.Clamp(MoveTarget.X, 0, (CurrentMap.Layers[0].Width - 1) * TileMap.TileSize);
            MoveTarget.Y = MathHelper.Clamp(MoveTarget.Y, 0, (CurrentMap.Layers[0].Height - 1) * TileMap.TileSize);

            destinationCell.X = (int)(MoveTarget.X / TileMap.TileSize);
            destinationCell.Y = (int)(MoveTarget.Y / TileMap.TileSize);

            if (CurrentMap.Layers[1].GetCellIndex(destinationCell.X, destinationCell.Y) != -1)
            {
                MoveTarget = MapPosition;
            }
        }

        public override void Draw(SpriteBatch batch)
        {
            if (InBattle)
            {
                batch.Draw(Texture, Position, drawColor);
                foreach (BattleText text in CombatText)
                    text.Draw(batch);
            }
            else
                batch.Draw(Texture, MapPosition, drawColor);
        }

        public override void Attack(Creature target)
        {
            base.Attack(target);
        }

        public void LevelUp()
        {
            NextLevel += Stats.Level * 1000;

            Stats.Level++;
            
            //Increment stats by some amount
        }
    }
}
